﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Globalization;
using System.Collections;
using System.Web;

namespace Lilium.Web.Mvc.MicrosoftAjax
{
	internal sealed class ClientCultureInfo
	{
		public static CultureInfo GetClientCulture(HttpContextBase context)
		{
			if (context == null)
				throw new ArgumentNullException("context");
			var langs = context.Request.UserLanguages;
			if (langs.Length != 0)
				try
				{
					return CultureInfo.GetCultureInfo(langs[0]);
				}
				catch (ArgumentException)
				{
				}
			return CultureInfo.InvariantCulture;
		}

		// Fields
		private static Hashtable cultureScriptBlockCache = Hashtable.Synchronized(new Hashtable());
		public DateTimeFormatInfo dateTimeFormat;
		private static readonly CultureInfo enUS = CultureInfo.GetCultureInfo(0x409);
		public string name;
		public NumberFormatInfo numberFormat;

		// Methods
		private ClientCultureInfo(CultureInfo cultureInfo)
		{
			this.name = cultureInfo.Name;
			this.numberFormat = cultureInfo.NumberFormat;
			this.dateTimeFormat = cultureInfo.DateTimeFormat;
		}

		internal static string GetClientCultureScriptBlock()
		{
			return GetClientCultureScriptBlock(CultureInfo.CurrentCulture);
		}

		internal static string GetClientCultureScriptBlock(CultureInfo cultureInfo)
		{
			if ((cultureInfo == null) || cultureInfo.Equals(enUS))
			{
				return null;
			}
			object obj2 = cultureScriptBlockCache[cultureInfo];
			if (obj2 == null)
			{
				ClientCultureInfo o = new ClientCultureInfo(cultureInfo);
				var serializer = new JavaScriptSerializer();
				string str = serializer.Serialize(o);
				if (str.Length > 0)
				{
					obj2 = "var __cultureInfo = '" + str + "';";
				}
				cultureScriptBlockCache[cultureInfo] = obj2;
			}
			return (string)obj2;
		}
	}
}